/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.uah.es.snomedctsubsetextractor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Luis
 */
public class Decomposer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String strLine;
        
        System.out.println("::::::::START - Decomposer::::::::");
        
        
        try {
            BufferedReader inSubsetA = null;

            BufferedReader inSubsetB = null;

            PrintStream outSubsetAminusB = null;

            PrintStream outSubsetAinterB = null;

            PrintStream outSubsetBminusA = null;

            String path = System.getProperty("user.dir") + File.separator + "etc" + File.separator;

            //inSubsetA = new BufferedReader(new FileReader(System.getProperty("user.dir") + File.separator + "etc" + File.separator + "CancerOvario-Subset.txt"));
            inSubsetA = new BufferedReader(new FileReader(path + args[0]));
            
            String subsetA = args[0].substring(0, args[0].length()-4); 
            
            String subsetB = args[1].substring(0,args[1].length()-4); 

            //inSubsetB = new BufferedReader(new FileReader(System.getProperty("user.dir") + File.separator + "etc" + File.separator + "CancerOvario-Subset-Expand.txt"));
            inSubsetB = new BufferedReader(new FileReader(path + args[1]));

            outSubsetAminusB = new PrintStream(new FileOutputStream( path + subsetA + "--" + subsetB + ".txt"));

            outSubsetAinterB = new PrintStream(new FileOutputStream( path + subsetA + "&&" + subsetB + ".txt"));

            outSubsetBminusA = new PrintStream(new FileOutputStream( path + subsetB + "--" + subsetA + ".txt"));


            // Lista de conceptos
            HashMap<Integer, String> conceptsA = new HashMap<Integer, String>();
            HashMap<Integer, String> conceptsB = new HashMap<Integer, String>();


            //Read File Line By Line
            while ((strLine = inSubsetA.readLine()) != null) {

                String[] arrayConcept = strLine.split(";");

                conceptsA.put(Integer.valueOf(arrayConcept[0]), arrayConcept[1]);
                
            }
            
            System.out.println("Conceptos A: "+conceptsA.size());

            //Close the input stream
            inSubsetA.close();

            //Read File Line By Line
            while ((strLine = inSubsetB.readLine()) != null) {

                String[] arrayConcept = strLine.split(";");

                // Print the content on the console
                conceptsB.put(Integer.valueOf(arrayConcept[0]), arrayConcept[1]);

            }
            
            System.out.println("Conceptos B: "+conceptsB.size());

            //Close the input stream
            inSubsetB.close();

            Iterator it = conceptsA.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry e = (Map.Entry) it.next();
                
                if (conceptsB.containsKey((Integer)e.getKey())) {
                    outSubsetAinterB.println(e.getKey() + ";" + e.getValue());
                    conceptsB.remove((Integer)e.getKey());
                } else {
                    outSubsetAminusB.println(e.getKey() + ";" + e.getValue());
                }

            }
            
            System.out.println("Conceptos B-A: "+conceptsB.size());

            it = conceptsB.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry e = (Map.Entry) it.next();
                outSubsetBminusA.println(e.getKey() + ";" + e.getValue());
            }


            System.out.println("::::::::THE END - Decomposer::::::::");

        } catch (IOException ex) {
            Logger.getLogger(Decomposer.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}
